/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainMalfunctionLog;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanContentExt;
import org.springblade.modules.repair.entity.MaintainPlanContentUser;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.mapper.MaintainMalfunctionLogMapper;
import org.springblade.modules.repair.service.IMaintainMalfunctionLogService;
import org.springblade.modules.repair.service.IMaintainPlanContentExtService;
import org.springblade.modules.repair.service.IMaintainPlanContentService;
import org.springblade.modules.repair.service.IMaintainPlanContentUserService;
import org.springblade.modules.repair.vo.MaintainMalfunctionLogVO;
import org.springblade.modules.repair.vo.WorkTrackVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 故障维修记录 服务实现类
 *
 * @author BladeX
 * @since 2021-05-21
 */
@Service
public class MaintainMalfunctionLogServiceImpl extends BaseServiceImpl<MaintainMalfunctionLogMapper, MaintainMalfunctionLog> implements IMaintainMalfunctionLogService {

	@Autowired
	@Lazy
	private IMaintainPlanContentService maintainPlanContentService;

	@Autowired
	@Lazy
	private IMaintainPlanContentExtService maintainPlanContentExtService;


	@Autowired
	@Lazy
	private IMaintainPlanContentUserService maintainPlanContentUserService;

	@Override
	public IPage<MaintainMalfunctionLogVO> selectMaintainMalfunctionLogPage(IPage<MaintainMalfunctionLogVO> page, MaintainMalfunctionLogVO maintainMalfunctionLog) {
		return page.setRecords(baseMapper.selectMaintainMalfunctionLogPage(page, maintainMalfunctionLog));
	}

	@Override
	public void insertWorkLog(Long planId, String typeName, Integer planStatus) {
		BladeUser user = AuthUtil.getUser();
		this.insertWorkLog(planId, typeName, planStatus, null, user.getUserId(), user.getNickName());
	}

	@Override
	public void insertWorkLog(Long planId, String typeName, Integer planStatus, String ext) {
		BladeUser user = AuthUtil.getUser();
		this.insertWorkLog(planId, typeName, planStatus, ext, user.getUserId(), user.getNickName());
	}

	@Override
	public void insertWorkLog(Long planId, String typeName, int planStatus, String ext, String tenantId) {
		MaintainMalfunctionLog maintainMalfunctionLog = new MaintainMalfunctionLog();
		maintainMalfunctionLog.setPlanId(planId);
		maintainMalfunctionLog.setTypeName(typeName);
		maintainMalfunctionLog.setPlanStatus(planStatus);
		maintainMalfunctionLog.setTenantId(tenantId);
		maintainMalfunctionLog.setExt(ext);
		this.save(maintainMalfunctionLog);
	}

	@Override
	public void insertWorkLog(Long planId, String typeName, Integer planStatus, String ext, Long executorId, String executor) {
		this.insertWorkLog(planId, typeName, planStatus, ext, executorId, executor, null);
	}

	@Override
	public void insertWorkLog(Long planId, String typeName, Integer planStatus, String ext, Long executorId, String executor, String userNames) {
		MaintainMalfunctionLog maintainMalfunctionLog = new MaintainMalfunctionLog();
		maintainMalfunctionLog.setPlanId(planId);
		maintainMalfunctionLog.setTypeName(typeName);
		maintainMalfunctionLog.setPlanStatus(planStatus);
		maintainMalfunctionLog.setExecutor(executor);
		maintainMalfunctionLog.setExecutorId(executorId);
		maintainMalfunctionLog.setTenantId(AuthUtil.getTenantId());
		maintainMalfunctionLog.setExt(ext);
		maintainMalfunctionLog.setUserNames(userNames);
		this.save(maintainMalfunctionLog);
	}

	@Override
	public WorkTrackVO selectWorkTracks(Long planId) {
		MaintainPlanContent planContent = maintainPlanContentService.getById(planId);
		if (planContent == null) {
			throw new ServiceException("未找到相关任务工单");
		}

		WorkTrackVO workTrack = new WorkTrackVO();
		workTrack.setTypeStatus(planContent.getTypeStatus());
		List<MaintainMalfunctionLog> workLogList = this.list(Wrappers.<MaintainMalfunctionLog>lambdaQuery()
			.eq(MaintainMalfunctionLog::getPlanId, planId)
			.eq(MaintainMalfunctionLog::getIsDeleted, BladeConstant.DB_NOT_DELETED)
			.orderByAsc(BaseEntity::getCreateTime, BaseEntity::getId));
		List<Integer> passStatus = new ArrayList<>();
		// 找不到枚举类，先这样跑起来再说
		passStatus.add(3);
		passStatus.add(4);
		passStatus.add(5);
		List<Integer> notPass = new ArrayList<>();
		notPass.add(8);
		notPass.add(9);
		for (int i = 0; i < workLogList.size(); i++) {
			MaintainMalfunctionLog maintainMalfunctionLog = workLogList.get(i);
			Integer planStatus = maintainMalfunctionLog.getPlanStatus();
			if (!Func.isNull(planStatus) && notPass.contains(planStatus)) {
				maintainMalfunctionLog.setIsPassed(0);
			}
			if (!Func.isNull(planStatus) && passStatus.contains(planStatus) && i != workLogList.size() - 1) {
				maintainMalfunctionLog.setIsPassed(1);
			}

			workLogList.set(i, maintainMalfunctionLog);
		}

		MaintainPlanContentExt planContentExt = maintainPlanContentExtService.getOne(Wrappers.<MaintainPlanContentExt>lambdaQuery().eq(MaintainPlanContentExt::getPlanId, planId).eq(MaintainPlanContentExt::getIsDeleted, BladeConstant.DB_NOT_DELETED), false);
		if (planContentExt != null) {
			workTrack.setTakeUpTime(String.valueOf(planContentExt.getWorkMinute()));
			workTrack.setWorkDuration(String.valueOf(planContentExt.getWorkDuration()));
		}
		workTrack.setWorkLogs(workLogList);

		workTrack.setTaskStatus(TaskStatusEnum.getTaskStatusByType(planContent.getTypeStatus()));

		List<MaintainPlanContentUser> list = maintainPlanContentUserService.list(Wrappers.<MaintainPlanContentUser>lambdaQuery().eq(MaintainPlanContentUser::getPlanId, planId));
		workTrack.setList(list);
		return workTrack;
	}

	@Override
	public List<MaintainMalfunctionLog> listByPlanId(Long planId) {
		return baseMapper.listByPlanId(planId);
	}
}
